<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{Order, Message as Messages, User as Users};
use think\Facade\Config as Configs;
use app\facade\Mns;
use think\Db;
use app\facade\Redis;

class Message extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Message')->setDescription('公众号消息通知');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }
    /** 微信模板  OPENTM405627405  OPENTM418383404 OPENTM417875155  OPENTM201449108 */
    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/duang/think Message'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);

        //订单接单通知
        (new Messages())->where(['message' => 1])->chunk(10, function ($lists) {
            foreach ($lists as $message) {
                $frtimes = Redis::get('tomessage' . $message['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('tomessage' . $message['id'], 30, 30);
                }
                $order = (new Order())->where('id', $message['orderid'])->find();
                $ordersn = $order['ordersn'] ?? 0;
                //通知售后
                if ($message['afconfirm'] == 1) {
                    $openids = (new Users())->whereNotNull('openid')->where(['id' => $order['suid'], 'type' => 2])->column('openid');
                }
                //通知财务
                if ($message['fconfirm'] == 1) {
                    $openids = (new Users())->whereNotNull('openid')->where(['groups' => $message['groups'], 'type' => 3])->column('openid');
                }
                //通知仓库
                if ($message['wconfirm'] == 1) {
                    $openids = (new Users())->whereNotNull('openid')->where(['groups' => [99, $message['groups']], 'type' => 4])->column('openid');
                }
                //通知管理员
                if ($message['mconfirm'] == 1) {
                    $openids = (new Users())->whereNotNull('openid')->where(['groups' => [99, $message['groups']], 'type' => 9])->column('openid');
                }
                //通知超级管理员
                if ($message['swconfirm'] == 1) {
                    $openids = (new Users())->whereNotNull('openid')->where(['groups' => [99, $message['groups']], 'type' => 10])->column('openid');
                }
                $text = '';
                if (!empty($message['text'])) {
                    $text =  $message['text'] ?? '无';
                    if (strlen($text) >= 20) {
                        $text = mb_substr($text, 0, 17, "utf-8") . "...";
                    }
                }
                $data = [
                    'character_string1' => [
                        'value' => $ordersn ?? 0
                    ],
                    'time2' => [
                        'value' =>  $message['created'] ?? date('Y-m-d H:i:s')
                    ],
                    'thing7' => [
                        'value' => $name ?? '无'
                    ],
                    'thing5' => [
                        'value' =>  $text ?? ''
                    ]
                ];
                foreach ($openids as $openid) {
                    (new Messages())->message($openid, '4noOu3H1gHaD2487TF0dFULGLOppClmXdYPXi4ek6Fc', $data, config('wechat.')['app_id'], '/pages/index/index?keywords=' . $ordersn);
                }
                //更新通知状态
                (new Messages())->where(['id' => $message['id']])->update(['message' => 2]);
            }
        });
    }
}
